#include <QtVector3DManager.h>


QtVector3DManager::QtVector3DManager(QObject *parent /*= 0*/ ) : QtAbstractPropertyManager(parent)
{
    m_doublePropertyManager = new QtDoublePropertyManager(this);

    connect(m_doublePropertyManager, SIGNAL(valueChanged(QtProperty*,double)),
                this, SLOT(slotDoubleChanged(QtProperty*,double)));

}

QtVector3DManager::~QtVector3DManager()
{

}

QVector3D QtVector3DManager::value(QtProperty *property_ptr)
{
    return m_propertyPtrToVector3D.value(property_ptr, QVector3D());
}

QtDoublePropertyManager *QtVector3DManager::subDoublePropertyManager() const
{
    return m_doublePropertyManager;
}

void QtVector3DManager::slotDoubleChanged(QtProperty *property, double value)
{
    if (QtProperty *xprop = m_XToproperty.value(property, 0)) {
        QVector3D p = m_propertyPtrToVector3D[xprop];
        p.setX(value);
        setValue(xprop, p);
    } else if (QtProperty *yprop = m_YToproperty.value(property, 0)) {
        QVector3D p = m_propertyPtrToVector3D[yprop];
        p.setY(value);
        setValue(yprop, p);
    } else if (QtProperty *zprop = m_ZToproperty.value(property, 0)) {
        QVector3D p = m_propertyPtrToVector3D[zprop];
        p.setZ(value);
        setValue(zprop, p);
    } else if (QtProperty *stepProp = m_SingleStepToproperty.value(property, 0)) {
        m_doublePropertyManager->setSingleStep(m_propertyToX[stepProp],value);
        m_doublePropertyManager->setSingleStep(m_propertyToY[stepProp],value);
        m_doublePropertyManager->setSingleStep(m_propertyToZ[stepProp],value);

    }
}

void QtVector3DManager::setValue(QtProperty *property_ptr, const QVector3D &value_vector3d)
{
    const QMap<const QtProperty *, QVector3D>::iterator it = m_propertyPtrToVector3D.find(property_ptr);
    if (it == m_propertyPtrToVector3D.end())
        return;

    if (it.value() == value_vector3d)
        return;

    it.value() = value_vector3d;
    m_doublePropertyManager->setValue(m_propertyToX[property_ptr], value_vector3d.x());
    m_doublePropertyManager->setValue(m_propertyToY[property_ptr], value_vector3d.y());
    m_doublePropertyManager->setValue(m_propertyToZ[property_ptr], value_vector3d.z());

    emit propertyChanged(property_ptr);
    emit valueChanged(property_ptr, value_vector3d);
}

void QtVector3DManager::initializeProperty(QtProperty *property)
{
    m_propertyPtrToVector3D[property] = QVector3D();

    QtProperty *singleStepProp = m_doublePropertyManager->addProperty();
    singleStepProp->setPropertyName(tr("SingleStep"));
    m_doublePropertyManager->setValue(singleStepProp, 1);
    m_propertyToSingleStep[property] = singleStepProp;
    m_SingleStepToproperty[singleStepProp] = property;
    property->addSubProperty(singleStepProp);


    QtProperty *xProp = m_doublePropertyManager->addProperty();
    xProp->setPropertyName(tr("X"));
    m_doublePropertyManager->setValue(xProp, 0);
    m_propertyToX[property] = xProp;
    m_XToproperty[xProp] = property;
    property->addSubProperty(xProp);

    QtProperty *yProp = m_doublePropertyManager->addProperty();
    yProp->setPropertyName(tr("Y"));
    m_doublePropertyManager->setValue(yProp, 0);
    m_propertyToY[property] = yProp;
    m_YToproperty[yProp] = property;
    property->addSubProperty(yProp);

    QtProperty *zProp = m_doublePropertyManager->addProperty();
    zProp->setPropertyName(tr("Z"));
    m_doublePropertyManager->setValue(zProp, 0);
    m_propertyToZ[property] = zProp;
    m_ZToproperty[zProp] = property;
    property->addSubProperty(zProp);

    //
    m_doublePropertyManager->setDecimals(xProp, 6);
    m_doublePropertyManager->setDecimals(yProp, 6);
    m_doublePropertyManager->setDecimals(zProp, 6);


}

QString QtVector3DManager::valueText(const QtProperty *property) const
{
    const QMap<const QtProperty *, QVector3D>::const_iterator it = m_propertyPtrToVector3D.constFind(property);
    if (it == m_propertyPtrToVector3D.constEnd())
        return QString();
    const QVector3D v = it.value();
    //return tr("(%1, %2)").arg(QString::number(v.x())).arg(QString::number(v.y()));
    return QString("Vector2D value");
}

#include "moc_QtVector3DManager.cxx"

